<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Đặng Khánh Trung
 * Date: 11/2/12
 * Time: 11:30 AM
 * To change this template use File | Settings | File Templates.
 */
// No direct access.
defined('_JEXEC') or die;

// Include dependancy of the main controllerform class
jimport('joomla.application.component.controllerform');

class VastControllerBaocaophong extends JControllerForm
{

    public function getModel($name = '', $prefix = '', $config = array('ignore_request' => true))
    {
        return parent::getModel($name, $prefix, array('ignore_request' => false));
    }

    public function submit()
    {
        // Check for request forgeries.
        JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

        $phongid = JRequest::getInt('phongid');
        $type = JRequest::getInt('baocaoLoai', 0);
                                
        if ($type == 1)
        {
            $this->setRedirect(JRoute::_('index.php?option=com_vast&view=baocaophongvehuu&phongid=' . $phongid));
        }
        elseif ($type == 2)
        {
            $this->setRedirect(JRoute::_('index.php?option=com_vast&view=baocaophongkhenthuong&phongid=' . $phongid));            
        }
        return true;        
    }
    
    public function csvVeHuuReport()
    {
        $tuoiNam = JRequest::getInt('nam', 60);
        $tuoiNu = JRequest::getInt('nu', 55);
        $phongid = JRequest::getInt('phongid');
        
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.hoten AS `Họ tên`, a.ngaysinh AS `Ngày sinh`, a.nguyenquan AS `Nguyên quán`, a.dienthoai AS `Số điện thoại`, a.thudientu AS `Thư điện tử`');
        $query->from('#__hosocanbo AS a');
        $query->join('INNER', '#__congtac AS b ON b.hosocanbo_id = a.id');
        $query->join('INNER', '#__phong AS c ON c.id = b.phong_id');
        $query->where('c.id = ' . (int)$phongid);
        
        $query->where('(DATEDIFF(CURDATE(), `ngaysinh`)/365 >= ' . $tuoiNu . ' AND `gioitinh` = 0) OR (DATEDIFF(CURDATE(), `ngaysinh`)/365 >= ' . $tuoiNam . ' AND `gioitinh` = 1)');
        
        $db->setQuery($query);
        
        $result = $db->loadObjectList();        
                
        if (count($result) > 0)
        {
            mb_internal_encoding("UTF-8");
            header('Content-Type: text/csv');
            header('Content-Disposition: attachment;filename=vastreport.csv');
            
            if ($fp = fopen('php://output', 'w')) {
                if ($result[0]) {
                    fputcsv($fp, array_keys(JArrayHelper::fromObject($result[0])));                    
                }
                foreach ($result as $row) {
                    fputcsv($fp, JArrayHelper::fromObject($row));                
                }
                fclose($fp);
            }        
            
            JFactory::getApplication()->close();
        }
        else
        {
            return false;
        }
    }

    public function csvKhenThuongReport()
    {       
        $phongid = JRequest::getInt('phongid');
        
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.hoten AS `Họ tên`, a.ngaysinh AS `Ngày sinh`, d.lydo AS `Lý do khen thưởng`, d.ngayquyetdinh AS `Ngày quyết định`, a.thudientu AS `Thư điện tử`');
        $query->from('#__hosocanbo AS a');
        $query->join('INNER', '#__congtac AS b ON b.hosocanbo_id = a.id');
        $query->join('INNER', '#__phong AS c ON c.id = b.phong_id');
        $query->join('INNER', '#__khenthuong AS d ON d.hosocanbo_id = a.id');
        $query->where('b.taivi = 1');
        $query->where('c.id = ' . $phongid);
                
        $db->setQuery($query);
        
        $result = $db->loadObjectList();        
                
        if (count($result) > 0)
        {
            mb_internal_encoding("UTF-8");
            header('Content-Type: text/csv');
            header('Content-Disposition: attachment;filename=vastreport.csv');
            
            if ($fp = fopen('php://output', 'w')) {
                if ($result[0]) {
                    fputcsv($fp, array_keys(JArrayHelper::fromObject($result[0])));                    
                }
                foreach ($result as $row) {
                    fputcsv($fp, JArrayHelper::fromObject($row));                
                }
                fclose($fp);
            }        
            
            JFactory::getApplication()->close();
        }
        else
        {
            return false;
        }
    }
    
}